﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using PGMHelper;

namespace BeamRein
{
    public class Storey
    {
        /// <summary>
        /// 抬头信息
        /// </summary>
        private string header { set; get; }

        /// <summary>
        /// 梁信息
        /// </summary>
        private List<Beam> beamList { set; get; }

        /// <summary>
        /// 配筋信息
        /// </summary>
        public ReinforceInfo Reinforce { set; get; }

        /// <summary>
        /// 本层面积
        /// </summary>
        public string Area { set; get; }

        /// <summary>
        /// 单方含钢量
        /// </summary>
        public string ReinforceArea { set; get; }

        /// <summary>
        /// 楼层编号
        /// </summary>
        public int StoreyIndex
        {
            get
            {
                var strList = WilsonHelper.SplitStr(this.header, new string[] { " ", "层梁钢筋用量统计", "第" });
                return Convert.ToInt32(strList.First());
            }
        }

        /// <summary>
        /// 楼层名
        /// </summary>
        public string Name
        {
            get
            {
                return string.Format("第{0}层", this.StoreyIndex);
            }
        }

        /// <summary>
        /// 获得梁集合
        /// </summary>
        /// <param name="beamEnum"></param>
        /// <returns></returns>
        private List<Beam> GetBeams(BeamEnum beamEnum)
        {
            return (from beam in this.beamList
                    where beam.GetBeamType() == beamEnum
                    select beam).ToList();
        }

        /// <summary>
        /// 楼层配筋信息
        /// </summary>
        /// <param name="beamEnum"></param>
        /// <returns></returns>
        public ReinforceInfo GetReinforceInfo(BeamEnum beamEnum)
        {
            //初始化
            var tRein = new ReinforceInfo(GlobalParam.BeamReinforceCount);
            //遍历目标梁求和
            this.GetBeams(beamEnum).ForEach(beam => tRein.Add(beam.Reinforce));
            //求和完成
            return tRein;
        }

        /// <summary>
        /// 梁统计
        /// </summary>
        /// <param name="beamEnum"></param>
        /// <returns></returns>
        public GridViewDatas GetBeamData()
        {
            //初始化表格
            var data = new GridViewDatas(this.Name + "用量统计");
            //获得楼层求和信息
            var reinList = (from beam in this.beamList select beam.Reinforce).ToList();
            //获得所有楼层名
            data.Add(new GridViewColumn(this.beamList.GetPropertyList("Name"), "连续梁名称"));
            //添加数据
            ReinforceInfo.ListToData(data, reinList);
            //返回统计信息
            return data;
        }

        /// <summary>
        /// 楼层信息
        /// </summary>
        /// <param name="filePath"></param>
        public Storey(string filePath)
        {
            //初始化
            this.beamList = new List<Beam>();
            this.Reinforce = new ReinforceInfo();
            //读取所有行
            var strList = File.ReadAllLines(filePath, Encoding.Default).ToList();
            //第一行
            this.header = WilsonHelper.SplitStr(strList.First()).First();
            //行序号
            int lineIndex = 1;
            //起始符号
            while(true)
            {
                //越界
                if (lineIndex >= strList.Count) break;
                //是否存在目标信息
                if (strList[lineIndex].Contains(GlobalParam.ImpLineStr))
                {
                    lineIndex++; break;
                }
                else lineIndex++;
            }
            //读取行
            for(;lineIndex < strList.Count; lineIndex++)
            {
                //是否存在目标信息
                if (strList[lineIndex].Contains(GlobalParam.ImpLineStr))
                {
                    lineIndex++; break;
                }
                //构造梁
                var beam = new Beam(strList[lineIndex]);
                //添加梁
                this.beamList.Add(beam);
            }
            //本层信息
            this.Reinforce.SetStoreyInfo(strList[lineIndex]);
            var lastStrList = WilsonHelper.SplitStr(strList.Last(), new string[] { " ", "," });
            this.Area = lastStrList[2].Replace("m2", "");
            this.ReinforceArea = lastStrList.Last().Replace("kg/m2", "");
        }


    }
}
